import { observable, action ,runInAction,toJS} from "mobx";


// 订单管理
export default class Order{
  
  @observable loading=false;
  
  // paging 数据分页
  @observable paging={data:[]};
  // where查询条件
  @observable where={}; 
  // 查询分类
  @observable tid=0; 
  
  constructor(root){
    this.root = root;
  }

  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){
          obj[temp]={};
        }
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        try{
          obj[temp] = v;
        }catch(e){
          console.log("error:obj[temp]",`obj[${temp}]`);
        }
        
      }
    };
    loop(this,arr,v);
  };
  
  // 初始化
  @action init=()=>this._init();
  _init(){
    this.set("loading",false);
    this.set("where",{});
    this.set("paging",{data:[]});
    this._paging(1);
  }
  
  // 获取分页
  @action paging=p=>this._paging(p);
  _paging(p){
    this.set("loading",true);
    p = p?p:1;
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-order-paging",
        p:p,
        field:this.where.field,
        value:this.where.value,
        condition:this.where.condition
      },
      success:json=>{
        this.set("loading",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        json.data.data = json.data.data.map(item=>{
          item.params=JSON.parse(item.params);
          return item;
        })
        this.set("paging",json.data);
      }
    });
  }

  // 删除
  @action delte=id=>this._delete(id);
  _delete(id){
    this.set("loading",true);
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-order-delete",
        id:id
      },
      success:json=>{
        this.set("loading",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this._init();
      }
    });
  }

    // 搜索
  @action search=()=>{
    this.set("paging",{data:[]});
    this._paging(1);
  }

}